<?php
/**
* @file $Id: Exams.php 161 2006-09-07 06:21:17Z doritojones $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

DrawHeader('Setup '.ProgramTitle());
if($_REQUEST['tables'] && $_POST['tables'])
{
	$table = $_REQUEST['table'];
	foreach($_REQUEST['tables'] as $id=>$columns)
	{
		if($id!='new')
		{
			$sql = "UPDATE $table SET ";

			foreach($columns as $column=>$value)
				$sql .= $column."='".str_replace("\'","''",$value)."',";

			$sql = substr($sql,0,-1) . " WHERE ID='$id'";
			$go = true;
		}
		else
		{
			$sql = "INSERT INTO $table ";
			
			if($table=='ASSESSMENT_SECTIONS')
			{
				$id = DBGet(DBQuery("SELECT ".db_seq_nextval('ASSESSMENT_SECTIONS_SEQ').' AS ID '.FROM_DUAL));
				$id = $id[1]['ID'];
				$fields = "ID,EXAM_ID,SYEAR,";
				$values = $id.",'$_REQUEST[exam_id]','".UserSyear()."',";
				$_REQUEST['section_id'] = $id;
			}
			elseif($table=='ASSESSMENT_EXAMS')
			{
				$id = DBGet(DBQuery("SELECT ".db_seq_nextval('ASSESSMENT_EXAMS_SEQ').' AS ID '.FROM_DUAL));
				$id = $id[1]['ID'];
				$fields = "ID,SYEAR,";
				$values = $id.",'".UserSyear()."',";
				$_REQUEST['exam_id'] = $id;
			}
			
			$go = false;

			foreach($columns as $column=>$value)
			{				
				if($value)
				{
					$fields .= $column.',';
					$values .= "'".str_replace("\'","''",$value)."',";
					$go = true;
				}
			}
			$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';
		}

		if($go)
			DBQuery($sql);
	}
	unset($_REQUEST['tables']);
}

if($_REQUEST['modfunc']=='delete')
{
	if($_REQUEST['section_id'])
	{
		$table = 'section';
		$sql = "DELETE FROM ASSESSMENT_SECTIONS WHERE ID='$_REQUEST[section_id]'";
	}
	else
	{
		$table = 'exam';
		$sql = "DELETE FROM ASSESSMENT_EXAMS WHERE ID='$_REQUEST[exam_id]'";
	}

	if(DeletePrompt($table))
	{
		DBQuery($sql);
		if(!$_REQUEST['section_id'])
		{
			DBQuery("DELETE FROM ASSESSMENT_SECTIONS WHERE EXAM_ID='$_REQUEST[exam_id]'");
			unset($_REQUEST['exam_id']);
		}
		else
			unset($_REQUEST['section_id']);
	
		unset($_REQUEST['modfunc']);
	}
}

if(!$_REQUEST['modfunc'])
{
	if($_REQUEST['section_id']!='new' && $_REQUEST['exam_id']!='new')
		$delete_button = "<INPUT type=button value=Delete onClick='javascript:window.location=\"Modules.php?modname=$_REQUEST[modname]&modfunc=delete&exam_id=$_REQUEST[exam_id]&section_id=$_REQUEST[section_id]\"'>";

	// ADDING & EDITING FORM
	if($_REQUEST['section_id'] && $_REQUEST['section_id']!='new')
	{
		$sql = "SELECT EXAM_ID,TITLE,SHORT_NAME,MAX_SCORE
				FROM ASSESSMENT_SECTIONS
				WHERE ID='$_REQUEST[section_id]'";
		$QI = DBQuery($sql);
		$RET = DBGet($QI);
		$RET = $RET[1];
		$title = $RET['TITLE'];
	}
	elseif($_REQUEST['exam_id'] && $_REQUEST['exam_id']!='new' && $_REQUEST['section_id']!='new')
	{
		$sql = "SELECT TITLE,SHORT_NAME,MAX_SCORE
				FROM ASSESSMENT_EXAMS
				WHERE ID='$_REQUEST[exam_id]'";
		$QI = DBQuery($sql);
		$RET = DBGet($QI);
		$RET = $RET[1];
		$title = $RET['TITLE'];
	}
	elseif($_REQUEST['section_id']=='new')
		$title = 'New Section';
	elseif($_REQUEST['exam_id']=='new')
		$title = 'New Exam';
	
	if($_REQUEST['section_id'])
	{
		echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&exam_id=$_REQUEST[exam_id]";
		if($_REQUEST['section_id']!='new')
			echo "&section_id=$_REQUEST[section_id]";
		echo "&table=ASSESSMENT_SECTIONS method=POST>";

		DrawHeader($title,$delete_button.'<INPUT type=submit value=Save>');
		$header .= '<TABLE cellpadding=3 bgcolor=#F0F0F1 width=100%>';
		$header .= '<TR>';

		$header .= '<TD>' . TextInput($RET['SHORT_NAME'],'tables['.$_REQUEST['section_id'].'][SHORT_NAME]','Short Name') . '</TD>';
		$header .= '<TD>' . TextInput($RET['TITLE'],'tables['.$_REQUEST['section_id'].'][TITLE]','Title') . '</TD>';
		$header .= '<TD>' . TextInput($RET['MAX_SCORE'],'tables['.$_REQUEST['section_id'].'][MAX_SCORE]','Maximum Score',' size=4 maxlength=7') . '</TD>';
		$header .= '</TR>';
		$header .= '</TABLE>';
	}
	elseif($_REQUEST['exam_id'])
	{
		echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&table=ASSESSMENT_EXAMS";
		if($_REQUEST['exam_id']!='new')
			echo "&exam_id=$_REQUEST[exam_id]";
		echo " method=POST>";
		DrawHeader($title,$delete_button.'<INPUT type=submit value=Save>');
		$header .= '<TABLE cellpadding=3 bgcolor=#F0F0F1 width=100%>';
		$header .= '<TR>';

		$header .= '<TD>' . TextInput($RET['SHORT_NAME'],'tables['.$_REQUEST['exam_id'].'][SHORT_NAME]','Short Name') . '</TD>';
		$header .= '<TD>' . TextInput($RET['TITLE'],'tables['.$_REQUEST['exam_id'].'][TITLE]','Title') . '</TD>';
		$header .= '<TD>' . TextInput($RET['MAX_SCORE'],'tables['.$_REQUEST['exam_id'].'][MAX_SCORE]','Maximum Score',' size=4 maxlength=7') . '</TD>';
	
		$header .= '</TR>';
		$header .= '</TABLE>';
	}
	else
		$header = false;

	if($header)
	{
		DrawHeader($header);
		echo '</FORM>';	
	}

	// DISPLAY THE MENU
	$LO_options = array('save'=>false,'search'=>false,'add'=>true);
	
	echo '<TABLE><TR>';
	
	// EXAMS
	$sql = "SELECT ID,TITLE FROM ASSESSMENT_EXAMS WHERE SYEAR='".UserSyear()."' ORDER BY TITLE";
	$QI = DBQuery($sql);
	$types_RET = DBGet($QI);
	
	if(count($types_RET))
	{
		if($_REQUEST['exam_id'])
		{
			foreach($types_RET as $key=>$value)
			{
				if($value['ID']==$_REQUEST['exam_id'])
					$types_RET[$key]['row_color'] = Preferences('HIGHLIGHT');
			}
		}
	}
		
	echo '<TD valign=top>';
	$columns = array('TITLE'=>'Exam');
	$link = array();
	$link['TITLE']['link'] = "Modules.php?modname=$_REQUEST[modname]&modfunc=$_REQUEST[modfunc]";
	$link['TITLE']['variables'] = array('exam_id'=>'ID');
	$link['add']['link'] = "Modules.php?modname=$_REQUEST[modname]&exam_id=new";

	ListOutput($types_RET,$columns,'Exam','Exams',$link,array(),$LO_options);
	echo '</TD>';

	// SECTIONS
	if($_REQUEST['exam_id'] && $_REQUEST['exam_id']!='new' && count($types_RET))
	{
		$sql = "SELECT ID,TITLE FROM ASSESSMENT_SECTIONS WHERE SYEAR='".UserSyear()."' AND EXAM_ID='".$_REQUEST['exam_id']."' ORDER BY TITLE";
		$QI = DBQuery($sql);
		$assn_RET = DBGet($QI);
		
		if(count($assn_RET))
		{
			if($_REQUEST['section_id'] && $_REQUEST['section_id']!='new')
			{
				foreach($assn_RET as $key=>$value)
				{
					if($value['ID']==$_REQUEST['section_id'])
						$assn_RET[$key]['row_color'] = Preferences('HIGHLIGHT');
				}
			}
		}
			
		echo '<TD valign=top>';
		$columns = array('TITLE'=>'Section');
		$link = array();
		$link['TITLE']['link'] = "Modules.php?modname=$_REQUEST[modname]&exam_id=$_REQUEST[exam_id]";
		$link['TITLE']['variables'] = array('section_id'=>'ID');
		$link['add']['link'] = "Modules.php?modname=$_REQUEST[modname]&exam_id=$_REQUEST[exam_id]&section_id=new";
	
		ListOutput($assn_RET,$columns,'Section','Sections',$link,array(),$LO_options);

		echo '</TD>';
	}
	
	echo '</TR></TABLE>';
}

function _makePercent($value,$column)
{
	return Percent($value,2);
}

?>